package com.yuelao.yuelao_backend.controller;

import com.yuelao.yuelao_backend.common.ApiResponse;
import com.yuelao.yuelao_backend.common.BizException;
import com.yuelao.yuelao_backend.common.ErrorCode;
import com.yuelao.yuelao_backend.dto.PostDTO;
import com.yuelao.yuelao_backend.entity.Topic;
import com.yuelao.yuelao_backend.service.TopicService;
import com.yuelao.yuelao_backend.util.UserContextUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import java.util.List;

/**
 * 话题Controller
 */
@RestController
@RequestMapping("/topics")
@Slf4j
@RequiredArgsConstructor
public class TopicController {

    private final TopicService topicService;

    /**
     * 获取话题列表
     * GET /topics
     * @return 话题列表
     */
    @GetMapping
    public ApiResponse<List<Topic>> getTopics() {
        List<Topic> topics = topicService.getAllTopics();
        return ApiResponse.ok(topics);
    }

    /**
     * 获取话题详情
     * GET /topics/{topicId}
     * @param topicId 话题ID
     * @return 话题详情
     */
    @GetMapping("/{topicId}")
    public ApiResponse<Topic> getTopicDetail(@PathVariable Long topicId) {
        if (topicId == null || topicId <= 0) {
            throw new BizException(ErrorCode.PARAMS_ERROR, "话题ID无效");
        }
        Topic topic = topicService.getTopicDetail(topicId);
        return ApiResponse.ok(topic);
    }

    /**
     * 创建话题
     * POST /topics/create
     * @param request 创建话题请求
     * @return 是否成功
     */
    @PostMapping("/create")
    public ApiResponse<Boolean> createTopic(@Valid @RequestBody CreateTopicRequest request) {
        Long userId = UserContextUtil.getCurrentUserId();
        boolean result = topicService.createTopic(
            request.getName(), 
            request.getDescription(), 
            request.getCoverImage(),
            userId
        );
        return ApiResponse.ok(result);
    }

    /**
     * 获取话题下的动态
     * GET /posts/topic/{topicId}
     * @param topicId 话题ID
     * @param page 页码
     * @param size 每页数量
     * @return 动态列表
     */
    @GetMapping("/{topicId}/posts")
    public ApiResponse<List<PostDTO>> getTopicPosts(
            @PathVariable Long topicId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        if (topicId == null || topicId <= 0) {
            throw new BizException(ErrorCode.PARAMS_ERROR, "话题ID无效");
        }
        List<PostDTO> posts = topicService.getTopicPosts(topicId, page, size);
        return ApiResponse.ok(posts);
    }

    /**
     * 加入话题
     * POST /topics/{topicId}/join
     * @param topicId 话题ID
     * @return 是否成功
     */
    @PostMapping("/{topicId}/join")
    public ApiResponse<Boolean> joinTopic(@PathVariable Long topicId) {
        if (topicId == null || topicId <= 0) {
            throw new BizException(ErrorCode.PARAMS_ERROR, "话题ID无效");
        }
        Long userId = UserContextUtil.getCurrentUserId();
        boolean result = topicService.joinTopic(userId, topicId);
        return ApiResponse.ok(result);
    }

    /**
     * 创建话题请求DTO
     */
    @lombok.Data
    public static class CreateTopicRequest {
        @NotBlank(message = "话题名称不能为空")
        private String name;
        
        @NotBlank(message = "话题描述不能为空")
        private String description;
        
        /**
         * 封面图URL
         */
        private String coverImage;
    }
}

